package com.lusoft.onepiece.activity;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FixedResolutionPolicy;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.SimpleLayoutGameActivity;

import android.os.Bundle;
import android.util.DisplayMetrics;

import com.lusoft.onepiece.R;
import com.lusoft.onepiece.constant.GameConstant;
import com.lusoft.onepiece.manager.MonsterManager;

public class Fight extends SimpleLayoutGameActivity {

	private Camera camera;
	
	private Scene scene;
	
	private BitmapTextureAtlas autoParallaxBackgroundTexture;
	
	private ITextureRegion parallaxLayerBack;
	
	private ITiledTextureRegion heroRegion;
	
	private AnimatedSprite hero;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		return new LimitedFPSEngine(pEngineOptions, 120);
	}


	@Override
	public EngineOptions onCreateEngineOptions() {
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		GameConstant.Resolution.WIDTH = dm.widthPixels;
		GameConstant.Resolution.HEIGHT = dm.heightPixels - 350;
		
		/**
		 * 1,可能需要相机追踪主角:只需要设置要追踪的实体: camera.setChaseEntity(entity)
		 * 2,设置相机的边界: 需要使用BoundCamera类或者其扩展类
		 * camera.setBounds(0, 0, 2000, 780);camera.setBoundsEnabled(true);
		 * 也就是说,相机不会超过2000, 780
		 * */
		camera = new Camera(0, 0, GameConstant.Resolution.WIDTH, GameConstant.Resolution.HEIGHT);
		/**
		 * 第一个参数：是否全屏
		 * 第二个参数：屏幕方向——横竖屏设置
		 * 第三个参数：
		 * FillResolutionPolicy 拉伸游戏画面为全屏填充，视摄像机大小不同，会有不同程度变形
		 * FixedResolutionPolicy 强行规定游戏画面为固定大小，此设置不会自动适应屏幕大小
		 * RatioResolutionPolicy 按比例修正画面大小，以适应屏幕大小
		 * RelativeResolutionPolicy RelativeResolutionPolicy
		 * 第四个参数：摄像机，这个一般多用SmoothCamera，除非你不需要有缩放效果。
		 * */
		EngineOptions options = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FixedResolutionPolicy(GameConstant.Resolution.WIDTH, GameConstant.Resolution.HEIGHT), camera);
		return options;
	}

	@Override
	public void onCreateResources(){
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		this.autoParallaxBackgroundTexture = new BitmapTextureAtlas(this.getTextureManager(), 2048, 2048);
		this.parallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.autoParallaxBackgroundTexture, this, "background.png", 0, 0);
		this.autoParallaxBackgroundTexture.load();
		
		/**
		 * AndEngine要求纹理加载的图片必须为2的整数次幂：new Texture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		 * */
		BitmapTextureAtlas heroTexture = new BitmapTextureAtlas(this.getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.heroRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(heroTexture, this, "hero.png", 0, 0, 1, 1);
		heroTexture.load();
		
		int heroX = (GameConstant.Resolution.WIDTH - 128)/2;
		int heroY = GameConstant.Resolution.HEIGHT - 256;
		hero = new AnimatedSprite(heroX, heroY, 128, 128, this.heroRegion, this.getVertexBufferObjectManager());
		
		MonsterManager.getInstance().init(this, hero);
	}

	@Override
	public Scene onCreateScene(){
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		scene = new Scene();
		
		for(int i=0;i<GameConstant.Layer.NUM;i++) {
			scene.attachChild(new Entity());
		}
		
		AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
		autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(10.0f, new Sprite(0, 0, this.parallaxLayerBack, this.getVertexBufferObjectManager())));
		scene.setBackground(autoParallaxBackground);
		
		MonsterManager.getInstance().show(scene.getChildByIndex(GameConstant.Layer.LAYER_MONSTER));
		
		scene.getChildByIndex(GameConstant.Layer.LAYER_MONSTER).attachChild(hero);
		
		scene.registerUpdateHandler(new IUpdateHandler(){

			@Override
			public void onUpdate(float second) {
				MonsterManager.getInstance().dealAttack();
			}

			@Override
			public void reset() {
				
			}
			
		});
		
		return scene;
	}

	protected int getLayoutID() {
		return R.layout.fight;
	}

	protected int getRenderSurfaceViewID() {
		return R.id.fightSurfaceView;
	}

}
